package top.archiesean.common.redis.config;

import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;

import org.redisson.config.Config;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;


/**
 * @author ArchieSean
 * @description 分布式锁配置类
 * @date 2023-12-31 23:38
 */
@AutoConfiguration
@ConfigurationProperties(prefix = "spring.data")
public class RedissonConfig {

    @Resource
    private RedisProperties redisProperties;

    /**
     * redisson 连接bean
     *
     * @return RedissonClient
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        //单节点 示例
        config.useSingleServer().setAddress("redis://" + redisProperties.getHost()
                + ":" + redisProperties.getPort()).setPassword(redisProperties.getPassword());


        /**
         //集群,此处根据需要自行配置
         config.useClusterServers()
         .addNodeAddress("")
         .setPassword("");
         */
        return Redisson.create(config);
    }
}
